# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution	
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv, fields

class car_project(osv.osv):
    _name = 'car.project'
    _columns = {
        'project_id': fields.char('Project ID', size = 10),
        'project_name': fields.char('Project name', size=50),
    }
car_project()

class car_company(osv.osv):
    _name = 'car.company'
    _rec_name = 'company_name'
    _columns = {
        'company_id': fields.char('Company ID', size=10),
        'company_name': fields.char('Company name', size=50),
        'address': fields.text('Address ', size = 150),
	    'tel': fields.integer('Tel'),
    }
car_company()

class car_car(osv.osv):
    _name = 'car.car'
    _rec_name = "no_car"
    def name_get(self, cr, uid, ids, context={}):
        if not len(ids):
            return []
        res = []
        reads = self.read(cr, uid, ids, ['no_car','driver_name'], context)
        for record in reads:
            if record:
                name =record['no_car'] +" - " + record['driver_name']
                res.append((record['id'], name))

        return res
    _columns = {
	     'no_car' : fields.char('Car No', size = 10,select=1,required=True),
		 'type_car' : fields.char('Type of car', size = 20,select=2),
		 'company_id' : fields.many2one('res.partner','Company',select=2),
		 'project_id' : fields.many2one('kdvn.project','Project'),
		 'driver_name' : fields.char('Driver name', size = 50),
		 'monthbasicfee' :fields.float('Month basic fee'),
		 'workinghour' : fields.datetime('Working hour'),
         'car_work_ids':fields.one2many('car.work','no_car','Car Work')
	}
car_car()

class car_work(osv.osv):
    _name = 'car.work'
    
    def _get_ovetime(self,cr,uid,ids,name,args,context):
        res= {}
        from datetime import datetime
        for cw in self.browse(cr,uid,ids):
           # raise osv.except_osv("E",cw.finishtime)
            #from_d = datetime.strptime( cw.starttime, '%Y-%m-%d %H:%M:%S')
            #to_d = datetime.strptime( cw.finishtime, '%Y-%m-%d %H:%M:%S')
          #  raise osv.except_osv("E",to_d - from_d )
            res[cw.id] = cw.finishkm - cw.startkm            
        return res
    
    _columns = {
            'working_day' : fields.date('Working day',select=1),
			'no_car' : fields.many2one('car.car','No car',select=1),
			'starttime' : fields.datetime('Start Time',select=1),
			'finishtime' : fields.datetime('Finish time',select=1),
			'startkm' : fields.integer('Start Km',select=1),
			'finishkm' : fields.integer('Finish Km',select=1),
            'over_time':fields.function(_get_ovetime,type='float',method=True,string='Over Time')
        }
car_work()			

